﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Imports NUnit.Framework
Imports SqlPlastic

<TestFixture()> _
Public Class CommandLineTests

    <Test()> _
    Public Sub ParseServerTest()
        Dim cmdLine() As String = {"/server:localhost"}
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual("localhost", context.Server)
    End Sub

    <Test()> _
    Public Sub ParseDatabaseTest()
        Dim cmdLine() As String = "/server:localhost /database:Northwind".Split(" "c)
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual("Northwind", context.Database)
    End Sub

    <Test()> _
    Public Sub ParseQuotedTest()
        Dim cmdLine() As String = {"/server:""local host""", "/database:Northwind"}
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual("local host", context.Server)
    End Sub

    <Test()> _
    Public Sub AllArgsTest()
        Dim cmdLine() As String = {"/server:localhost", "/database:Northwind", "/username:user", "/password:pwd", "/outputxmlpath:output.dbml", "/outputcodepath:mycodefile.cs", "/archivepath:archive.dbml", "/namespace:MyNamespace", "/sqlmetallocation:C:\sqlmetal.exe", "/language:cs"}
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual("localhost", context.Server)
        Assert.AreEqual("archive.dbml", context.ArchiveFilePath)
        Assert.AreEqual("cs", context.CodeFileExtension)
        Assert.AreEqual("Northwind", context.Database)
        Assert.AreEqual("MyNamespace", context.GeneratedNamespace)
        Assert.AreEqual(Language.CSharp, context.Language)
        Assert.AreEqual("mycodefile.cs", context.OutputCodeFilePath)
        Assert.AreEqual("output.dbml", context.OutputXmlFilePath)
        Assert.AreEqual("pwd", context.Password)
        Assert.AreEqual("C:\sqlmetal.exe", context.SqlMetalLocation)
        Assert.AreEqual("user", context.UserName)
    End Sub

    <Test()> _
    Public Sub NoArgsTest()
        Dim sqlmetal As String = IO.Path.Combine( _
                                       IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName, _
                                       "Microsoft.NET\Framework\v3.5\sqlmetal.exe")
        Dim cmdLine() As String = {} '"/server:localhost", "/database:Northwind", "/username:user", "/password:pwd", "/outputxmlpath:output.dbml", "/outputcodepath:mycodefile.cs", "/archivepath:archive.dbml", "/namespace:MyNamespace", "/sqlmetallocation:C:\sqlmetal.exe", "/language:cs"}
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual("", context.Server)
        Assert.AreEqual("output.original.xml", context.ArchiveFilePath)
        Assert.AreEqual("cs", context.CodeFileExtension)
        Assert.AreEqual("", context.Database)
        Assert.AreEqual("Generated.By.SqlPlastic", context.GeneratedNamespace)
        Assert.AreEqual(Language.CSharp, context.Language)
        Assert.AreEqual("output.cs", context.OutputCodeFilePath)
        Assert.AreEqual("output.xml", context.OutputXmlFilePath)
        Assert.AreEqual("", context.Password)
        Assert.AreEqual(sqlmetal, context.SqlMetalLocation)
        Assert.AreEqual("", context.UserName)
    End Sub

    <Test()> _
    Public Sub VBTest()
        Dim cmdLine() As String = {"/language:vb"}
        Dim parser As New CommandLineParser
        Dim context As New AppContext
        parser.Parse(cmdLine, context)
        Assert.AreEqual(Language.VB, context.Language)
        Assert.AreEqual("vb", context.CodeFileExtension)
    End Sub



End Class
